#ifndef _SURFACE_PANEL_H_
#define _SURFACE_PANEL_H_

#include "SurfaceSource.h"
#include "ControlPanelBase.h"
#include "ui_SurfaceProperty.h"
#include "DisplayModel.h"
#include "PipelineModel.h"

#include "vtkPlane.h"
#include "vtkClipPolyData.h"
#include "vtkPlaneWidget.h"
#include "vtkImplicitPlaneWidget2.h"
#include "vtkImplicitPlaneWidget.h"
#include "vtkImplicitPlaneRepresentation.h"

#include <QRgb>
#include <QColor>
#include <QColorDialog>


class SurfacePanel : public ControlPanelBase
{
	Q_OBJECT

public:
	SurfacePanel(QWidget *parent) : ControlPanelBase(parent)
	{
		m_ui.setupUi(this);

		m_panelType = ptSurface;
		m_ssource=NULL;
		m_planeWidget=NULL;

		connect(m_ui.btn_contourApply, SIGNAL(clicked()), SLOT(isoApply()));
		connect(m_ui.btn_opacityApply, SIGNAL(clicked()), SLOT(opacityApply()));
		connect(m_ui.btn_selectColor, SIGNAL(clicked()), SLOT(selectColor()));

		connect(m_ui.spin_tranx, SIGNAL(valueChanged(int)), SLOT(tranChanged()));
		connect(m_ui.spin_trany, SIGNAL(valueChanged(int)), SLOT(tranChanged()));
		connect(m_ui.spin_tranz, SIGNAL(valueChanged(int)), SLOT(tranChanged()));
	}

	virtual void setSource(const QModelIndex& index)
	{
		ControlPanelBase::setSource(index);

		Source *source = currentSource();
		if ( source->type()!=stSurface)
		{
			qDebug() << "Not surface";
			m_ssource=NULL;
			return;
		}

		m_ssource = (SurfaceSource*)source;

		QColor color = m_ssource->surfaceColor();
		if ( !color.isValid())
			return;

		m_ui.spin_opacity->setValue(m_ssource->opacity());
		m_ui.label_color->setStyleSheet(QString::fromUtf8("background-color: rgb(%1, %2, %3);")
			.arg(color.red()).arg(color.green()).arg(color.blue()));

		double *pos = m_ssource->getPosition();

		if ( pos)
		{
			m_sourcePos[0] = pos[0];
			m_sourcePos[1] = pos[1];
			m_sourcePos[2] = pos[2];
		}
		else
		{
			m_sourcePos[0] = 0;
			m_sourcePos[1] = 0;
			m_sourcePos[2] = 0;
		}
	}


private slots:
	void isoApply()
	{
		if ( !m_ssource)
			return;

		int num = m_ui.spin_contourNum->value();
		double start = m_ui.spin_contourFrom->value();
		double end = m_ui.spin_contourTo->value();

		m_ssource->rebuildSurface(num, start, end);
	}


	void opacityApply()
	{
		if ( !m_ssource)
			return;

		m_ssource->setOpacity(m_ui.spin_opacity->value());
	}

	void selectColor()
	{
		if ( !m_ssource)
			return;


		QColor color = QColorDialog::getColor();

		m_ui.label_color->setStyleSheet(QString::fromUtf8("background-color: rgb(%1, %2, %3);")
			.arg(color.red()).arg(color.green()).arg(color.blue()));

		m_ssource->setSurfaceColor(color);
	}

	void tranChanged()
	{
		if ( !m_ssource)
			return;

		int x = m_ui.spin_tranx->value();
		int y = m_ui.spin_trany->value();
		int z = m_ui.spin_tranz->value();

		printf("%d,%d,%d\n", x, y, z);

		double position[3];

// 		if ( m_sourcePos[0] == -1 && 
// 			   m_sourcePos[1] == -1 &&
// 				 m_sourcePos[2] == -1)
// 		{
// 			double *pos = m_ssource->getPosition();
// 
// 			if ( pos)
// 			{
// 				m_sourcePos[0] = pos[0];
// 				m_sourcePos[1] = pos[1];
// 				m_sourcePos[2] = pos[2];
// 			}
// 		}

		position[0] = m_sourcePos[0];
		position[1] = m_sourcePos[1];
		position[2] = m_sourcePos[2];


		double *bounds = m_ssource->getBounds();
		if ( !bounds)
			return;

		printf("%f,%f,%f\n", m_sourcePos[0], m_sourcePos[1], m_sourcePos[2]);

		double dx = bounds[1] - bounds[0];
		double dy = bounds[3] - bounds[2];
		double dz = bounds[5] - bounds[4];

		dx = dx/1000.0;
		dy = dy/1000.0;
		dz = dz/1000.0;

		printf("%f,%f,%f\n", dx, dy, dz);

		position[0] += x*dx;
		position[1] += y*dy;
		position[2] += z*dz;

		printf("New:%f,%f,%f\n", position[0], position[1], position[2]);
				
		m_ssource->setPosition(position);
	}

private:
	Ui::SurfaceProperty m_ui;
	SurfaceSource *m_ssource;
	vtkPlaneWidget *m_planeWidget;

	double m_sourcePos[3];
};


#endif

